<?php
include_once '../model/Usuario.php';
include_once '../model/Comentario.php';
?>
<!DOCTYPE HTML>
<html>
    <head>
        <?php include 'content.php'; ?>
        <script type="text/javascript">
            $(document).ready(function () {
                
                if ($('#conf').val()>0){
                    charts();
                    $('#notPie').css("display","none");
                }
                
            });         
            
            function charts(){
                $('#pie').highcharts({
                    chart: {
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    title: {
                        text: 'Estadisticas de Clientes'
                    },
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: true,
                                color: '#000000',
                                connectorColor: '#000000',
                                format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                            }
                        }
                    },
                    series: [{
                            type: 'pie',
                            name: 'Clientes',
                            data: [
                                ['Conformes',   parseInt($('#conf').val())],
                                ['No Conformes',   parseInt($('#noConf').val())]
                            ]
                        }]
                });
            }
            
            function addComment(){
                                        
            
                if ($("#comment_add").val().trim()==""){
                    alert("Debe de agregar un comentario");
                }else if ($("#ok").val().trim()==0){
                    alert("Debe seleccionar si esta conforme con el avance");
                }else if (confirm('Este seguro de agregar el comentario: \n "'+$("#comment_add").val()+'"')){
                    $("#comments").append(" "+
                        "<div class='comment'>"+
                        "   <h4> " + $("#name").val() + ": </h4> "+
                        "   <p>"+$("#comment_add").val()+"</p>"+
                        "</div>");
                    $("#commentAdd").val($("#comment_add").val());
                    $("#comment_add").val("");
                    
                    $('#notPie').css("display","none");
                    
                    $.ajax({
                        data:  $("#form").serialize(),
                        url:   '<?php echo $_SERVER['HELPER'] ?>?action=comentarios&method=add&result=ajax',
                        type:  'post',
                        beforeSend: function () {
                        },
                        success:  function (data) {  
                            
                            var info = JSON.parse(data);                                                    
                            if (info.estatus == AJAX_SUCCESSFUL) {
                                $('#conf').val(info.ok);
                                $('#noConf').val(info.notok);  
                                
                                charts();
                            }                                    
                        }                                    
                    });                                        
                }            
                $("#comment_add").focus();
            }
            
            
        </script>
    </head>
    <body>      
        <?php include_once 'header.php'; ?> 
        <div id="main">
            <div class="column-left">
                <?php include_once 'lateral.php'; ?> 
            </div>
            <div class="column-center" style="text-align: right;">
                <div style="text-align: left;">
                    <h2>Comentarios del Avance</h2>
                </div>                
                <div id="load">
                    <fieldset id="ficha">
                        <legend>Ficha de Avance</legend>   
                        <iframe src="<?php echo $_SESSION["pdf"] ?>"
                                style="height: 400px; width: 100%">

                        </iframe>
                    </fieldset>
                </div>               
                <div style="text-align: left; margin-top: 20px;">  
                    <fieldset>
                        <legend>Agregar Comentario</legend>
                        <form id="form" name="form" method="post" action="">

                            <div id="add" style="text-align: right;">
                                <textarea id="comment_add" name="comment_add" rows="4" style="width: 100%;" maxlength="200"></textarea>                        
                                <br/><br/>
                                <label for="ok">Conforme: </label>
                                <select id="ok" name="ok" style="margin-right: 20px;">
                                    <option value="0">-</option>
                                    <option value="N">No</option>
                                    <option value="S">Si</option>
                                </select>
                                <img alt=""  src="../images/comment_add.png" width="32" height="32" class="pointer"
                                     onclick="addComment();"/>
                            </div>
                            <?php
                            $usuario = new Usuario();
                            $usuario = unserialize($_SESSION['usuario']);
                            ?>
                            <input type="hidden" id="name" name="name" value="<?php echo $usuario->getNombre() . ' ' . $usuario->getApellido() ?>"/>
                            <input type="hidden" id="conf" name="conf" value="<?php echo $_SESSION['ok'] ?>"/>
                            <input type="hidden" id="noConf" name="noConf" value="<?php echo $_SESSION['notok'] ?>"/>
                            <input type="hidden" id="commentAdd" name="commentAdd"/>
                        </form>
                    </fieldset>
                </div>
                <div style="text-align: left; margin-top: 20px;">  
                    <fieldset>
                        <legend>Estadisticas</legend>
                        <div id="notPie"><h3>No hay comentarios por los momentos</h3></div>
                        <div id="pie" style="min-width: 310px; height: 400px; margin: 0 auto"></div>                        
                    </fieldset>
                </div>
                <div style="text-align: left; margin-top: 20px;">  
                    <fieldset>
                        <legend>Comentarios del Avance</legend>
                        <div id="comments">
                            <?php
                            $listComentarios = unserialize($_SESSION['listComentarios']);
                            $i = 0;

                            for ($i = 0; $i < count($listComentarios); $i++) {
                                $comentario = new Comentario();
                                $comentario = $listComentarios[$i];
                                echo "<div class='comment'>
                                        <h4> " . $comentario->getUsuario()->getNombre() . " " . $comentario->getUsuario()->getApellido() . ": </h4> 
                                        <p>" . $comentario->getComentario() . "</p>
                                     </div>";
                            }
                            ?>
                        </div>
                    </fieldset>
                </div>
                <br/>
            </div>
            <div class="column-right">
                <?php include 'right.php'; ?> 
            </div>
        </div>
        <?php include_once 'footer.php'; ?>   
    </body>    
</html>